﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IObjects;
using Entities;
using DataObjects.EFData;
using Utilities;

namespace DataObjects
{
    public class Message : IMessage
    {
        private MessageInfo _messageInfo;
        private string mode = string.Empty;
        private WebOnlineEntities datacontext;
        #region implement interface
        public MessageInfo MessageInfo
        {
            get
            {
                return _messageInfo;
            }
            set
            {
                _messageInfo = value;
            }
        }

        public string Mode
        {
            get
            {
                return mode;
            }
            set
            {
                mode = value;
            }
        }

        public int Save()
        {
            if (mode == Constants.MODE_INSERT)
            {
                return Insert();
            }
            if (mode == Constants.MODE_UPDATE)
            {
                return Update();
            }
            if (mode == Constants.MODE_INSERT)
            {
                return Delete();
            }
            throw new Exception("Mode is invalid in Message object");
        }
        public IList<MessageInfo> GetMessages()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var messagesList = datacontext.TB_Message.Select(c => c.ToMessageInfo()).ToList();
                return messagesList;
            }
        }

        public IList<MessageInfo> GetMessagesInboxOfUser(string userId)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var messagesList = datacontext.TB_Message.Where(c => c.To == userId && !c.IsDeletedByReceiver).Select(c => c.ToMessageInfo()).ToList();
                return messagesList;
            }
        }

        public MessageInfo GetById(string id)
        {
            using (datacontext = new WebOnlineEntities())
            {
                Guid msgId = new Guid(id);
                var message = datacontext.TB_Message.Where(c => c.MessageId == msgId).FirstOrDefault();
                if (message != null)
                {
                    return message.ToMessageInfo();
                }
                return null;
            }
        }
        public IList<MessageInfo> GetDeletedMessagesInboxOfUser(string userId)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var messagesList = datacontext.TB_Message.Where(c => c.To == userId && c.IsDeletedByReceiver && !c.HideToReceiver).Select(c => c.ToMessageInfo()).ToList();
                return messagesList;
            }
        }
        public IList<MessageInfo> GetMessagesOutboxOfUser(string userId)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var messagesList = datacontext.TB_Message.Where(c => c.From == userId).Select(c => c.ToMessageInfo()).ToList();
                return messagesList;
            }
        }
        #endregion
        #region methods
        private int Delete()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var message = datacontext.TB_Message.Where(c => c.MessageId == _messageInfo.MessageId).FirstOrDefault();
                datacontext.DeleteObject(message);
                return datacontext.SaveChanges();
            }
        }

        private int Update()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var message = datacontext.TB_Message.Where(c => c.MessageId == _messageInfo.MessageId).FirstOrDefault();
                message.Content = _messageInfo.Content;
                message.From = _messageInfo.From;
                message.IsNew = _messageInfo.IsNew;
                message.Title = _messageInfo.Title;
                message.To = _messageInfo.To;
                message.IsDeletedByReceiver = _messageInfo.IsDeletedByReceiver;
                message.HideToReceiver = _messageInfo.HideToReceiver;
                return datacontext.SaveChanges();
            }
        }

        private int Insert()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.AddToTB_Message(_messageInfo.ToTB_Message());
                return datacontext.SaveChanges();
            }
        }
        #endregion 
    

       
    

        
    }
}
